package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.c1.C1Executor;
import com.lineage.server.templates.L1Name_Power;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C1_Name_Type_Table {
  private static final Log _log = LogFactory.getLog(C1_Name_Type_Table.class);
  
  private static final Map<Integer, HashMap<Integer, L1Name_Power>> _types = new HashMap<>();
  
  private static final Map<Integer, HashMap<Integer, Integer>> _typesLv = new HashMap<>();
  
  private static final Map<Integer, HashMap<Integer, Integer>> _typesLv_down = new HashMap<>();
  
  private static C1_Name_Type_Table _instance;
  
  public static C1_Name_Type_Table get() {
    if (_instance == null)
      _instance = new C1_Name_Type_Table(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    int i = 0;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `server_c1_name_type`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int c1_id = rs.getInt("c1_id");
        int c1_type = rs.getInt("c1_type");
        String c1_name_type = rs.getString("c1_name_type");
        String c1_classname = rs.getString("c1_classname");
        int set = rs.getInt("set");
        int down = rs.getInt("down");
        int int1 = rs.getInt("int1");
        int int2 = rs.getInt("int2");
        int int3 = rs.getInt("int3");
        int int4 = rs.getInt("int4");
        String c1_name_title = rs.getString("c1_name_title");
        int getscore = rs.getInt("getscore");
        L1Name_Power power = new L1Name_Power();
        power.set_c1_id(c1_id);
        power.set_c1_name_type(c1_name_type);
        power.set_c1_name_title(c1_name_title);
        C1Executor classname = power(c1_classname, int1, int2, int3, int4);
        if (classname != null) {
          power.set_c1_classname(classname);
          power.set_set(set);
          power.set_down(down);
          power.set_getscore(getscore);
          HashMap<Integer, L1Name_Power> types = _types.get(Integer.valueOf(c1_type));
          if (types == null)
            types = new HashMap<>(); 
          types.put(Integer.valueOf(c1_id), power);
          HashMap<Integer, Integer> typesLv = _typesLv.get(Integer.valueOf(c1_type));
          if (typesLv == null)
            typesLv = new HashMap<>(); 
          typesLv.put(Integer.valueOf(c1_id), Integer.valueOf(set));
          HashMap<Integer, Integer> typesLv_down = _typesLv_down
            .get(Integer.valueOf(c1_type));
          if (typesLv_down == null)
            typesLv_down = new HashMap<>(); 
          typesLv_down.put(Integer.valueOf(c1_id), Integer.valueOf(down));
          _types.put(Integer.valueOf(c1_type), types);
          _typesLv.put(Integer.valueOf(c1_type), typesLv);
          i++;
        } 
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("载入阵营阶级能力记录数量: " + i + "(" + timer.get() + "ms)");
  }
  
  public L1Name_Power get(int key1, int key2) {
    HashMap<Integer, L1Name_Power> powers = _types.get(Integer.valueOf(key1));
    if (powers != null)
      return powers.get(Integer.valueOf(key2)); 
    return null;
  }
  
  public HashMap<Integer, L1Name_Power> get(int key1) {
    HashMap<Integer, L1Name_Power> powers = _types.get(Integer.valueOf(key1));
    if (powers != null)
      return powers; 
    return null;
  }
  
  public int getLv(int key1, int score) {
    HashMap<Integer, Integer> powers = _typesLv.get(Integer.valueOf(key1));
    if (powers == null)
      return 0; 
    int i = powers.size();
    while (i > 0) {
      Integer ps = powers.get(Integer.valueOf(i));
      if (score >= ps.intValue())
        return i; 
      i--;
    } 
    return 0;
  }
  
  private C1Executor power(String className, int int1, int int2, int int3, int int4) {
    if (className.equals("0"))
      return null; 
    try {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.append("com.lineage.server.model.c1.");
      stringBuilder.append(className);
      Class<?> cls = Class.forName(stringBuilder.toString());
      C1Executor exe = (C1Executor)cls.getMethod("get", new Class[0]).invoke(null, new Object[0]);
      exe.set_power(int1, int2, int3, int4);
      return exe;
    } catch (ClassNotFoundException e6) {
      String error = "发生[阵营阶级能力档案]错误, 检查档案是否存在:" + className;
      _log.error(error);
    } catch (IllegalArgumentException e) {
      _log.error(e.getLocalizedMessage(), e);
    } catch (IllegalAccessException e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } catch (InvocationTargetException e3) {
      _log.error(e3.getLocalizedMessage(), e3);
    } catch (SecurityException e4) {
      _log.error(e4.getLocalizedMessage(), e4);
    } catch (NoSuchMethodException e5) {
      _log.error(e5.getLocalizedMessage(), e5);
    } 
    return null;
  }
}
